package com.motorhome.manage.dao.impl;

import com.motorhome.base.dao.Pager;
import com.motorhome.base.dao.impl.BaseDaoImpl;
import com.motorhome.base.util.StringUtil;
import com.motorhome.manage.dao.CarDao;
import com.motorhome.manage.domain.Car;
import com.motorhome.manage.domain.Price;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;

/**
 * @author: John
 */
@Repository
public class CarDaoImpl extends BaseDaoImpl implements CarDao {

    @Override
    public Pager<Car> getCarList(int page, int pageSize) {
        return queryForPageByPageNo("select t1.id,t1.name,t1.description,t1.status,t1.remark,t1.create_time," +
                        "t1.pic_url,t1.view_url,t1.insurance,t1.car_num,t1.device_id,t1.device_number,t1.device_name,t1.flag " +
                        "from m_car t1",
                new Object[]{}, new BeanPropertyRowMapper<>(Car.class), page, pageSize);
    }

    @Override
    public Pager<Car> getCarListType(int page, int pageSize,int type) {
        return queryForPageByPageNo("select t1.id,t1.name,t1.description,t1.status,t1.remark,t1.create_time," +
                        "t1.pic_url,t1.view_url,t1.insurance,t1.car_num,t1.device_id,t1.device_number,t1.device_name,t1.flag " +
                        "from m_car t1 where flag = "+type,
                new Object[]{}, new BeanPropertyRowMapper<>(Car.class), page, pageSize);
    }

    @Override
    public int addCar(String name, String description, int status, String remark, String picPath, String viewUrl, Float insurance, int carNum,int flag) {
        return update("insert into m_car(name,description,status,remark,pic_url,view_url,insurance,car_num) values(?,?,?,?,?,?,?,?,?)",
                name, description, status, remark, picPath, viewUrl, insurance, carNum,flag);
    }

    @Override
    public int updateCar(long id, String name, String description, int status, String remark, String picPath, String viewUrl, Float insurance, int carNum,int flag) {
        if (StringUtil.isBlank(picPath)) {
            return update("update m_car set name=?,description=?,status=?,remark=?,view_url=?,insurance=?,car_num=?,flag=? where id = ?",
                    name, description, status, remark, viewUrl, insurance, carNum, flag,id);
        } else {
            return update("update m_car set name=?,description=?,status=?,remark=?,pic_url=?,view_url=?,insurance=?,car_num=?,flag=? where id = ?",
                    name, description, status, remark, picPath, viewUrl, insurance, carNum, flag,id);
        }
    }

    @Override
    public int deleteCar(long id) {
        return update("delete from m_car where id = ?", id);
    }

    @Override
    public int insertPrice(long carId, String priceDesc, float price, int orderBy,
                           float rateWeekend,
                           float rate1, int day1,
                           float rate2, int day2,
                           float rate3, int day3,
                           float topVipRate) {
        return update("insert into m_price(car_id,price_desc,price,order_by,rate_weekend,rate1,day1,rate2,day2,rate3,day3,top_vip_rate)" +
                        " values(?,?,?,?,?,?,?,?,?,?,?,?)",
                carId, priceDesc, price, orderBy, rateWeekend, rate1, day1, rate2, day2, rate3, day3, topVipRate);
    }

    @Override
    public int deletePrice(long priceId) {
        return update("delete from m_price where id = ?", priceId);
    }

    @Override
    public List<Price> getCarPrice(List<Long> carIds) {
        MapSqlParameterSource parameterSource = new MapSqlParameterSource();
        parameterSource.addValue("param", carIds);

        return namedParameterJdbcTemplate.query("select id,car_id,price_desc,price,rate_weekend,rate1,day1,rate2,day2,rate3,day3,top_vip_rate from m_price " +
                        "where car_id in (:param) " +
                        "order by order_by",
                parameterSource,
                new BeanPropertyRowMapper<>(Price.class));
    }

    @Override
    public Car getCarByDeviceId(String deviceId, String deviceNumber) {
        return queryForObject("select t1.id,t1.name,t1.description,t1.status,t1.remark,t1.create_time," +
                "t1.pic_url,t1.view_url,t1.insurance,t1.car_num,t1.device_id,t1.device_number,t1.device_name " +
                        "from m_car t1 where t1.device_id = ? or t1.device_number = ?",
                new BeanPropertyRowMapper<Car>(Car.class), deviceId, deviceNumber);
    }

    @Override
    public int bindDevice(long carId, String deviceId, String deviceNumber, String deviceName) {
        return update("update m_car set device_id=?,device_number=?,device_name=? where id = ?",
                deviceId, deviceNumber, deviceName, carId);
    }

}
